--
-- DailyView.lua
-- Created by IntelliJ IDEA.
-- User: yao.chen
-- Date: 2018/12/27
--

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

local TabsBtn = require("app.widget.TabsBtn")
local DailyTaskView = import(".DailyTaskView")
local DailyWishWorld = import(".DailyWishWorld")
local DailyRarePanel = import(".DailyRarePanel")

ClassRef.VIEW_ROOT_RES = Res.DailyUiScene

function ClassRef:init()
    self._ui.Button_close:addClickEventListener(function()
        self:closeView()
    end)

    local btns = {0, 0, 0}
    local btnsY = {0, 0, 0}
    for i = 1, #btns do
        btns[i] = self._ui.Panel_tab:getChildByName("selected_btn_"..i)
        btnsY[i] = btns[i]:getPositionY()
    end
    self._btns = btns
    self._btnsY = btnsY --used to adjust tabBtn

    local function onTabsBtn(index)
        if self._curIdx then
            self:_switchPanel(index)
        else
            self._curIdx = index -- 创建tabsBtn的会默认调用一次
        end
    end

    self._bindPanel = {}

    self._tabsBtn = TabsBtn.new(btns, onTabsBtn, 1)

    self._taskPanel = nil

    self:refreshOpenBtnState()
end

function ClassRef:checkGuide()
    local funcId = -1
    if self._curIdx == 1 then
        funcId = Constant.FUNC_TYPE_DAILY -- FUNC_TYPE_DAILY_TASK
    elseif self._curIdx == 2 then
        funcId = Constant.FUNC_TYPE_DAILY_RARE  --宝物
    elseif self._curIdx == 3 then
        funcId = Constant.FUNC_TYPE_DAILY_WISH_WORLD    --祝福世界
    end

    local params = {
        scene = funcId,
        sceneSelf = self,
        getFunc = self.getGuideNodeFunc,
        nextFunc = self.checkGuide
    }
    EM:notify("checkGuide", params)
end

-- 获取引导node
function ClassRef:getGuideNodeFunc(id)
    if id == 281 then   --每日任务奖励提示
        return self._bindPanel[1]._ui["Panel/Panel_bom/reward_1/Panel"]
    end
end

function ClassRef:show(index)
    self:setVisible(true)
    self:_switchPanel(index)
    AM.playSoundById(7012)

    -- 检查红点
    UD.redPoint:checkStateByKey(Constant.FUNC_TYPE_DAILY_WISH_WORLD)
    self:refreshTabBtnRedPoints(Constant.FUNC_TYPE_DAILY_WISH_WORLD)

    self:refreshRareRedPoint()

    self:checkGuide()
end

function ClassRef:refresh(data)
    local panel = self._bindPanel[self._curIdx]
    panel:refresh(data)
end

function ClassRef:getTabByFuncId(funcId)
    if funcId == Constant.FUNC_TYPE_DAILY_TASK then
        return self._btns[1]
    elseif funcId == Constant.FUNC_TYPE_DAILY_RARE then
        return self._btns[2]
    elseif funcId == Constant.FUNC_TYPE_DAILY_WISH_WORLD then
        return self._btns[3]
    end
end

function ClassRef:refreshTabBtnRedPoints(funcId)
    local btn = self:getTabByFuncId(funcId)
    if not btn then return end
    local dot = btn:findChild("dot")
    dot:setVisible(UD.redPoint:getStateByKey(funcId))
end

-- 刷新右侧按钮显隐状态
function ClassRef:refreshOpenBtnState()
    if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_DAILY_TASK) then
        self._btns[1]:setVisible(true)
    else
        self._btns[1]:setVisible(false)
    end
    if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_DAILY_RARE) then
        self._btns[2]:setVisible(true)
    else
        self._btns[2]:setVisible(false)
    end
    if UIHandler:isFunctionOpen(Constant.FUNC_TYPE_DAILY_WISH_WORLD) then
        self._btns[3]:setVisible(true)
    else
        self._btns[3]:setVisible(false)
    end

    -- 动态调整posY
    local visibleBtnNum = 1 -- 可见按钮个数、默认为1
    for i = 1, #self._btns do
    	if self._btns[i]:isVisible() then
            self._btns[i]:setPositionY(self._btnsY[visibleBtnNum])
            visibleBtnNum = visibleBtnNum + 1
    	end
    end
end

function ClassRef:updateTaskProgress(taskType)
    if taskType == "daily" then
        local panel = self._bindPanel[self._curIdx]
        if not cc.isDead(panel) and panel.updateDailyTaskProgress then panel:updateDailyTaskProgress() end
        -- 更新红点
        UD.redPoint:checkStateByKey(Constant.FUNC_TYPE_DAILY_TASK)
        self:refreshTabBtnRedPoints(Constant.FUNC_TYPE_DAILY_TASK)
    end
end

function ClassRef:onGetDailyTaskAward(taskId)
    local panel = self._bindPanel[self._curIdx]
    if not cc.isDead(panel) and panel.updateDailyTasksShow then 
        panel:updateDailyTasksShow(taskId) 
    end

    -- 更新红点
    UD.redPoint:checkStateByKey(Constant.FUNC_TYPE_DAILY_TASK)
    self:refreshTabBtnRedPoints(Constant.FUNC_TYPE_DAILY_TASK)
end

function ClassRef:refreshDailyTask(pos)
    local panel = self._bindPanel[self._curIdx]
    if not cc.isDead(panel) and panel.refreshDailyTask then 
        panel:refreshDailyTask(pos) 
    end
end

function ClassRef:playDailyRefreshAni(pos)
    local panel = self._bindPanel[self._curIdx]
    if not cc.isDead(panel) and panel.playDailyRefreshAni then 
        panel:playDailyRefreshAni(pos) 
    end
end

-- 更新最新任务
function ClassRef:updateNewTasks(tasks)
    local panel = self._bindPanel[self._curIdx]
    if not cc.isDead(panel) and panel.updateNewTasks then 
        panel:updateNewTasks(tasks) 
    end
end

function ClassRef:refreshBlessTimes(blessTimes)
    local panel = self._bindPanel[self._curIdx]
    if not cc.isDead(panel) and panel.refreshBlessTimes then 
        panel:refreshBlessTimes(blessTimes) 
    end
end

function ClassRef:updatePrivilegeInfo(msg)
    local panel = self._bindPanel[self._curIdx]
    if not cc.isDead(panel) and panel.updatePrivilegeInfo then 
        panel:updatePrivilegeInfo(msg) 
    end
end

function ClassRef:refreshPrivilegeBox(msg)
    local panel = self._bindPanel[self._curIdx]
    if not cc.isDead(panel) and panel.refreshPrivilegeBox then 
        panel:refreshPrivilegeBox(msg) 
    end
end

function ClassRef:onSwitchPanel(cb)
    self._onSwitchPanel = cb
end

function ClassRef:_switchPanel(index)
    if index then
        self._curIdx = index
        self._tabsBtn:setSelected(index)
    end
    self:createPanel(self._curIdx)
    self:refresh()
    executeFunc(self._onSwitchPanel, self._curIdx)

    for k, v in pairs(self._bindPanel) do
        if v then
            if tonumber(k) == self._curIdx then
                v:setVisible(true)
            else
                v:setVisible(false)
            end
        end
    end
end

function ClassRef:createPanel(index)
    if not self._bindPanel[index] then
        local panel
        if index == 1 then
            --每日任务
            self._ui["Panel/task"]:removeAllChildren()
            panel = DailyTaskView.create()
            self._ui["Panel/task"]:addChild(panel)
            self._taskPanel = panel
        elseif index == 2 then
            -- 宝箱
            panel = DailyRarePanel.create(self._ui.rare)
        elseif index == 3 then
            --祝福世界
            panel = DailyWishWorld.create(self._ui.map)
        end

        if panel then
            if panel.onOperate then
                panel:onOperate(self._onOperate)
            end
            self._bindPanel[index] = panel
        end
    else
        self._bindPanel[index]:onOperate(self._onOperate)
    end
end

function ClassRef:setNodeVisible(isVisible)
    isVisible = isVisible or false
    if self._taskPanel then
        self._taskPanel:setVisible(isVisible)
    end
    self._ui.rare:setVisible(isVisible)
    self._ui.map:setVisible(isVisible)
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

function ClassRef:refreshRareRedPoint()
    -- 检查宝物红点
    UD.redPoint:checkStateByKey(Constant.FUNC_TYPE_DAILY_RARE)
    self:refreshTabBtnRedPoints(Constant.FUNC_TYPE_DAILY_RARE)
end

return ClassRef
